#!/usr/bin/python
# -*- coding:UTF-8 -*-
# filename:start.py

import web
from package import blog as b
from package import yyc
from package import session

urls = (
    '/blog', b.app_blog,
    '/yyc', yyc.app_yyc,
    '/session', session.app_session,
    '/reg', 'register',
    '/t/hello', 'templatehello',
    '/t/ctx', 'templatectx',
    '/(.*)', 'index'
)

app = web.application(urls, globals())
render = web.template.render('templates/')

class index:
    def GET(self, path):
        outstr = 'Welcome to new.i-david.org, URI is /{0}, path is {0}\n'.format(path)
        return outstr

class register:
    def POST(self):
        data = web.input()
        outstr = 'Register info \n'
        for key in data:
            outstr += key
            outstr += ': '
            outstr += data[key]
            outstr += '\n'
        return outstr

class templatehello:
    def GET(self):
        i = web.input(name='David')
        return render.hello(i.name)

class templatectx:
    def GET(self):
        c = web.ctx
        req = {
            'environ': c.env,
            'home': c.home,
            'homedomain': c.homedomain,
            'homepath': c.homepath,
            'host': c.host,
            'ip': c.ip,
            'method': c.method,
            'path': c.path,
            'protocol': c.protocol,
            'query': c.query,
            'fullpath': c.fullpath
        }
        res = {
            'status': c.status,
            'headers': c.headers,
            'output': c.output
        }
        ctx = {'Request': req, 'Response': res}
        return render.ctx(ctx)

if __name__ == '__main__':
    app.run()

